
#include <Arduino.h>
#include <U8g2lib.h>

#include "DHT.h"
#define DHTPIN 25 
#define DHTTYPE DHT11

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

//初始化屏幕显示
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE);   
DHT dht(DHTPIN, DHTTYPE);
float h,t;
void firstScreen(){
   u8g2.setFont(u8g2_font_6x10_tf);
   for(int offsetY= 0; offsetY<40; offsetY++){
      u8g2.clearBuffer();
      u8g2.drawStr( 0, 10, "OpenIotBox");
      u8g2.drawFrame(32,65-offsetY,64,32);
      u8g2.drawStr(34,84-offsetY,"EnvIOTDemo");
      delay(10);
      u8g2.sendBuffer();
   }
}

void draw(){
  u8g2.drawStr(36,12, "EnvIOTDemo");
  if (isnan(h) || isnan(t) ){
    u8g2.drawStr(64,32,"Error");
  }
  u8g2.drawStr(20,30,"Temp:");
  u8g2.drawStr(20,45,"Humid:");
  u8g2.drawStr(20,60,"MQTT:");
  u8g2.setCursor(50, 30);
  u8g2.print(t);
  u8g2.setCursor(55, 45);
  u8g2.print(h);
}

void setup() {
  dht.begin();
  u8g2.begin();
  firstScreen();
  delay(100);
}

void loop() {
  h = dht.readHumidity();
  t = dht.readTemperature();
  u8g2.clearBuffer();
  draw();
  
  u8g2.sendBuffer();
  delay(1000);
}
